<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/4/6
  Time: 16:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Js set Cookie</title>
    <script type="text/javascript">
        function getCookie(name){
            var str = document.cookie;
            if(!str || str.indexOf(name+"=")<0)
                    return 'null';
            var cookies = str.split("; ");
            for(var i = 0; i < cookies.length; i++){
                var cookie = cookies[i];
                if(cookie.indexOf(name + "=") == 0){
                    var value = cookie.substring(name.length+1);
                }else{
                    return 'this cookie not';
                }
                return name + '='+decodeURI(value);
            }
        }

        function setCookie(name,value){
            document.cookie = name + "=" + encodeURI(value);
        }

        function searchCookie(){
            var CookieS = document.getElementById('name1').value;
            alert(getCookie(CookieS));
        }
    </script>
</head>
<body>
<div style="margin:0 auto; width:60%;" align="center">
    <fieldset>
        <legend>当前有效的Cookie</legend>
        <div id="cookieDiv"></div>
        <script type="text/javascript">
            cookieDiv.innerHTML = document.cookie;
        </script>
    </fieldset>
    <fieldset>
        <legend>welcome to jsCookie</legend>
        <table align="center">
            <tr>
                <td>read Cookie:</td>
                <td><input name="name1" id="name1"/>
                    <input type="button"
                           value="read" onclick="searchCookie();"/>
                </td>
            </tr>
            <tr>
                <td>set Cookie</td>
                <td></td>
            </tr>
            <tr>
                <td align="right">Name attribute:</td>
                <td><input id="name2"/></td>
            </tr>
            <tr>
                <td align="right">Value attribute:</td>
                <td><input id="value2" /></td>
            </tr>
            <tr>
                <td> </td>
                <td>
                    <input type="button" value="set"
                           onclick="setCookie(document.getElementById('name2').value,document.getElementById('value2').value);
                           cookieDiv.innerHTML = document.cookie;"/>
                </td>
            </tr>
        </table>
    </fieldset>
</div>
</body>
</html>
